package com.jzg.jcptadmin.presenter;

import com.jzg.jcptadmin.base.BasePresenter;
import com.jzg.jcptadmin.constant.Constant;
import com.jzg.jcptadmin.data.DataManager;
import com.jzg.jcptadmin.data.vo.CheckPriceBean;
import com.jzg.jcptadmin.data.vo.EvaluateResultBean;
import com.jzg.jcptadmin.viewinterface.ReportInterface;

import java.util.Map;

import rx.Observer;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * @auther: steven
 * Email: lixp@jingzhengu.com
 * @time 2017/3/15 13:54
 * @desc: 价格预警Presenter
 */

public class ReportPresenter extends BasePresenter<ReportInterface> {
    private Subscription mSubscription;

    public void OperationEvaluationData(Map<String, String> params) {
        if (!checkSubmitInfo(params)) return;
        mSubscription = DataManager.getInstance().evaluateData(params)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io()).subscribe(new Observer<EvaluateResultBean>() {
                    @Override
                    public void onCompleted() {
                    }

                    @Override
                    public void onError(Throwable e) {
                        if (e != null) {
                            e.printStackTrace();
                            getMvpView().showError(Constant.errr_net);
                        }
                    }

                    @Override
                    public void onNext(EvaluateResultBean successData) {
                        getMvpView().evaluationSuccess(successData);
                    }
                });
    }

    public void checkPrice(Map<String, String> params) {
        mSubscription = DataManager.getInstance().checkPrice(params)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<CheckPriceBean>() {
                    @Override
                    public void onCompleted() {
                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                        getMvpView().showError(Constant.errr_net);
                    }

                    @Override
                    public void onNext(CheckPriceBean successData) {
                        getMvpView().checkPriceSuccess(successData);
                    }
                });
    }

    @Override
    public void detachView() {
        super.detachView();
        if (mSubscription != null) mSubscription.unsubscribe();
    }
}
